package com.ben.chatroom.handler;

import com.ben.chatroom.common.ProInstant;
import com.ben.chatroom.common.entity.ProtoMsg;
import com.ben.chatroom.session.ClientSession;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class LoginResponseHandler extends SimpleChannelInboundHandler<ProtoMsg.Message> {


    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ProtoMsg.Message message) throws Exception {
        ProtoMsg.HeadType type = message.getType();
        if (!ProtoMsg.HeadType.LOGIN_RESPONSE.equals(type)) {
            super.channelRead(ctx, message);
            return;
        }

        //判断是否登录成功
        ProtoMsg.LoginResponse response = message.getLoginResponse();
        ProInstant.ResultCode result = ProInstant.ResultCode.getResult(response.getCode());

        if (!ProInstant.ResultCode.SUCCESS.equals(result)) {
            log.error("登录Netty服务节点失败");
        } else {
            ClientSession session = ctx.channel().attr(ClientSession.SESSION_KEY).get();
            session.setSessionId(message.getSessionId());
            ctx.channel().attr(ClientSession.IS_LOGIN).set(true);

        }


    }
}
